package com.justreading.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author LYJ
 * @Description
 * @date 2021 年 10 月 31 日 下午2:10
 */
public class NettyByeBuf01 {
    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.buffer(10);
        for (int i = 0; i < 10; i++) {
            byteBuf.writeByte(i);
        }
        System.out.println("capacity=" + byteBuf.capacity());
        System.out.println(byteBuf.readerIndex());
        for (int i = 0; i < 10; i++) {
            System.out.print(byteBuf.readByte() + " ");
        }
        System.out.println("");
        System.out.println(byteBuf.readerIndex());


        ByteBuf buf = Unpooled.copiedBuffer("hello,world!", CharsetUtil.UTF_8);
        if (buf.hasArray()) {

            byte[] bytes = buf.array();
            System.out.println(new String(bytes, CharsetUtil.UTF_8));


            System.out.println(buf.arrayOffset()); // 0
            System.out.println(buf.readerIndex()); // 0
            System.out.println(buf.writerIndex()); // 12
            System.out.println(buf.capacity()); // 36


            int len = buf.readableBytes();
            System.out.println("len=" + len);


            System.out.println(buf.getCharSequence(0,4,CharsetUtil.UTF_8));
            System.out.println(buf.getCharSequence(4,3,CharsetUtil.UTF_8));
        }
    }
}
